package com.lyh.societiesmanager.controller;

import com.lyh.societiesmanager.ClubService.DepartmentService;
import com.lyh.societiesmanager.entity.Department;
import com.lyh.societiesmanager.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/departments")
@Api(tags = "部门管理接口")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("查询所有部门")
    @GetMapping
    public Response<List<Department>> getAllDepartments() {
        return Response.success(departmentService.list());
    }

    @ApiOperation("根据ID查询部门")
    @GetMapping("/{id}")
    public Response<Department> getDepartmentById(@PathVariable Integer id) {
        Department department = departmentService.getById(id);
        return department != null ? Response.success(department) : Response.error(404, "未找到该部门");
    }

    @ApiOperation("新增部门")
    @PostMapping
    public Response<Boolean> addDepartment(@RequestBody Department department) {
        return departmentService.save(department) ? Response.success("添加成功", true) : Response.error(500, "添加失败");
    }

    @ApiOperation("更新部门信息")
    @PutMapping("/{id}")
    public Response<Boolean> updateDepartment(@PathVariable Integer id, @RequestBody Department department) {
        department.setDepartmentId(id);
        return departmentService.updateById(department) ? Response.success("更新成功", true) : Response.error(500, "更新失败");
    }

    @ApiOperation("删除部门")
    @DeleteMapping("/{id}")
    public Response<Boolean> deleteDepartment(@PathVariable Integer id) {
        return departmentService.removeById(id) ? Response.success("删除成功", true) : Response.error(500, "删除失败");
    }
}